﻿package com.fastframework.net {
	import com.fastframework.error.ErrorLog;
	import com.fastframework.event.EventDispatcherUtils;

	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.utils.ByteArray;

	/**
	 * @author colin
	 */
	final public class LoadXML implements ILoader, IFASTEventDispatcher, ISendAndLoad {
		public static var isDebug:Boolean = false;
		private var base:URLLoader;
		private var xmlContent : XML;
		private var isLoading:Boolean = false;
	
		public function LoadXML(){
			base = new URLLoader();
			base.dataFormat = URLLoaderDataFormat.BINARY;
			base.addEventListener(Event.COMPLETE,loaded);
		}
		
		private function loaded(e:Event):void{
			isLoading = false;

			var receiveBytes:ByteArray = base.data;
			//try decompress data;
			try{
				receiveBytes.uncompress();
			}catch(e:Error){
				if(isDebug)ErrorLog.instance().log(e);
			}finally{}

			receiveBytes.position = 0;
			xmlContent = new XML(receiveBytes.readUTFBytes(receiveBytes.length));
			if(isDebug)trace(xmlContent.toXMLString());
		}

		public function getBase():XML{
			return xmlContent;
		}

		public function sendAndLoad(url : String, postData:URLVariables,method : String) : Boolean{
			var req:URLRequest = new URLRequest(url);
			req.method = method;
			req.data = postData;
			if(isDebug)trace(url);
			base.load(req);
			return true;
		};
/*
var Boundary:String = "---------------------------7d76d1b56035e"
var FormData:String = "--" + Boundary + '\r\nContent-Disposition: form-data; name="Filename"\r\n\r\nfileName1.txt\r\n'
FormData += "--" + Boundary + '\r\nContent-Disposition: form-data; name="Filedata"; filename="fileName1.txt"\r\nContent-Type: application/octet-stream\r\n\r\nContent of fileName1.txt\r\n';
FormData += "--" + Boundary + '\r\nContent-Disposition: form-data; name="Upload"\r\n\r\nSubmit Query\r\n'
FormData += "--" + Boundary + '--'

var loader:URLLoader = new URLLoader();
var AcceptHeader:URLRequestHeader = new URLRequestHeader("Accept","text/*");
var request:URLRequest = new URLRequest("http://www.yoursite.com/upload.php");
request.requestHeaders.push(AcceptHeader);

var dataArray:ByteArray = new ByteArray();
dataArray.writeMultiByte(FormData,"ascii");
request.data = dataArray;
request.method = URLRequestMethod.POST;
request.contentType = "multipart/form-data; boundary=" + Boundary;
loader.addEventListener("complete",onComplete);
try {
loader.load(request);
} catch (error:Error) {
trace("Unable to load requested document.");
}*/

		public function sendBinaryAndLoad(url:String, binary:ByteArray):Boolean{
			var req:URLRequest = new URLRequest(url);
			req.contentType = 'application/octet-stream';
			req.method = URLRequestMethod.POST;
			req.data = binary;
			if(isDebug)trace(url);
			base.load(req);
			return true;
		}

		public function load(requestURL : String):void{
			if(isLoading==true)base.close();
			if(isDebug)trace(requestURL);
			base.load(new URLRequest(requestURL));
		}
	
		public function getTargetContainer() : Object {
			return xmlContent;
		}
		
		public function unload() : void {
		}

		public function willTrigger(type : String) : Boolean{
			return base.willTrigger(type);
		};

		public function addEventListener(type : String, listener:Function, useCapture : Boolean = false, priority : int = 0, useWeakReference : Boolean = false) : void {
			base.addEventListener(type, listener,useCapture,priority,useWeakReference);
		};

		public function removeEventListener(type : String, listener : Function, useCapture : Boolean = false) : void {
			base.removeEventListener(type, listener,useCapture);
		};

		public function dispatchEvent(event : Event) : Boolean{
			return base.dispatchEvent(event);
		};
		
		public function hasEventListener(type : String) : Boolean{
			return base.hasEventListener(type);
		};
	
		public function when(eventType : String, whichObject : Object, callFunction : Function) : * {
			EventDispatcherUtils.when(this,eventType,whichObject,callFunction);
			return this;
		};
	}
}